<?php 

Yii::import('zii.widgets.CMenu');

class LsMenu
extends CMenu
{
	public $submenuItemTemplate;
	public $hideInactiveSubmenu = true;
	

	/**
	 * Renders the content of a menu item.
	 * Note that the container and the sub-menus are not rendered here.
	 * @param array $item the menu item to be rendered. Please see {@link items} on what data might be in the item.
	 * @since 1.1.6
	 */
	protected function renderMenuItem($item)
	{
		if(isset($item['url'])){
			$label=$this->linkLabelWrapper===null ? $item['label'] : '<'.$this->linkLabelWrapper.'>'.$item['label'].'</'.$this->linkLabelWrapper.'>';
			$result = CHtml::link($label,$item['url'],isset($item['linkOptions']) ? $item['linkOptions'] : array());
		}else{
			$result = CHtml::tag('span',isset($item['linkOptions']) ? $item['linkOptions'] : array(), $item['label']);
		}
		if(isset($item['append'])){
			$result .= $item['append'];
		}
		return $result;
	}
	
	/**
	 * Recursively renders the menu items.
	 * @param array $items the menu items to be rendered recursively
	 */
	protected function renderMenuRecursive($items, $level=0)
	{
		$count=0;
		$n=count($items);
		foreach($items as $item)
		{
			$count++;
			$options=isset($item['itemOptions']) ? $item['itemOptions'] : array();
			$class=array();
			if($item['active'] && $this->activeCssClass!='')
				$class[]=$this->activeCssClass;
			if($count===1 && $this->firstItemCssClass!='')
				$class[]=$this->firstItemCssClass;
			if($count===$n && $this->lastItemCssClass!='')
				$class[]=$this->lastItemCssClass;
			if($class!==array())
			{
				if(empty($options['class']))
					$options['class']=implode(' ',$class);
				else
					$options['class'].=' '.implode(' ',$class);
			}

			echo CHtml::openTag('li', $options);

			$menu=$this->renderMenuItem($item);
			$sub_tpl = false;
			if(isset($this->itemTemplate) || isset($item['template'])
			|| $sub_tpl = ($level && isset($this->submenuItemTemplate)))
			{
				$template=isset($item['template'])
					? $item['template']
					: ($sub_tpl
						? $this->submenuItemTemplate
						: $this->itemTemplate
					)
				;
				echo strtr($template,array('{menu}'=>$menu));
			}
			else
				echo $menu;
			
			if(!$this->hideInactiveSubmenu || ($this->hideInactiveSubmenu && $item['active'])){
				if(isset($item['items']) && count($item['items']))
				{
					echo "\n".CHtml::openTag('ul',isset($item['submenuOptions']) ? $item['submenuOptions'] : $this->submenuHtmlOptions)."\n";
					$this->renderMenuRecursive($item['items'], $level+1);
					echo CHtml::closeTag('ul')."\n";
				}
			}
			echo CHtml::closeTag('li')."\n";
		}
	}
}

?>